package org.example.springblog.service;

import org.example.springblog.mapper.BlogInfoMapper;
import org.example.springblog.model.BlogInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 九转苍翎
 * Date: 2025/8/3
 * Time: 20:05
 */
@Service
public class BlogService {

    @Autowired
    private BlogInfoMapper blogInfoMapper;

    public List<BlogInfo> getList() {
        List<BlogInfo> lists = blogInfoMapper.selectBlogInfoAll();
        for (BlogInfo curBlogInfo : lists) {
            String curContent = curBlogInfo.getContent();
            int length = curContent.length() - 1;
            curBlogInfo.setContent(curContent.substring(0, Math.min(length, 200)));
        }
        return lists;
    }

    public BlogInfo getBlogDetail(Integer blogId) {
        BlogInfo blogInfo = new BlogInfo();
        blogInfo.setId(blogId);
        return blogInfoMapper.selectBlogInfoById(blogInfo);
    }

    public Integer publish(BlogInfo blogInfo) {
        return blogInfoMapper.insertBlogInfo(blogInfo);
    }

    public Integer update(BlogInfo  blogInfo) {
        return blogInfoMapper.updateBlogInfo(blogInfo);
    }

    public Integer delete(Integer blogId) {
        BlogInfo blogInfo = new BlogInfo();
        blogInfo.setId(blogId);
        blogInfo.setDeleteFlag(1);
        return blogInfoMapper.updateBlogInfo(blogInfo);
    }
}
